﻿using ActiveStoryTouch.DataModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Windows.Ink;
using System.IO;
using System.Runtime.Serialization;
using ActiveStoryTouch.DataModel.Wpf;

namespace TestSuite
{
    
    
    /// <summary>
    ///This is a test class for SerializableStrokeCollectionTest and is intended
    ///to contain all SerializableStrokeCollectionTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SerializableStrokeCollectionTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for LoadFromFile
        ///</summary>
        [TestMethod()]
        public void LoadFromFileTest()
        {
            string fileName = CommonTestConstants.StrokeCollectionSerializationTestFileName;
            FileStream fs = new FileStream(Path.Combine(CommonTestConstants.RelativeTestProjectDirectory, "StrokeSerializationTest.isf"), FileMode.Open, FileAccess.Read, FileShare.Read);
            StrokeCollection sc = new StrokeCollection(fs);
            fs.Close();

            SerializableStrokeCollection expected = new SerializableStrokeCollection();

            foreach (var stroke in sc)
            {
                SerializableDrawingAttributes drawingAttr = new SerializableDrawingAttributes { Color = stroke.DrawingAttributes.Color,
                                                                                                FitToCurve = stroke.DrawingAttributes.FitToCurve,
                                                                                                Height = stroke.DrawingAttributes.Height,
                                                                                                Width = stroke.DrawingAttributes.Width,
                                                                                                IgnorePressure = stroke.DrawingAttributes.IgnorePressure,
                                                                                                IsHighlighter = stroke.DrawingAttributes.IsHighlighter,
                                                                                                StylusTipTransform = stroke.DrawingAttributes.StylusTipTransform
                                                                                              };
                switch (stroke.DrawingAttributes.StylusTip)
                {
                    case StylusTip.Ellipse:
                        drawingAttr.StylusTip = SerializableDrawingAttributes.StylusTips.Ellipse;
                        break;
                    case StylusTip.Rectangle:
                        drawingAttr.StylusTip = SerializableDrawingAttributes.StylusTips.Rectangle;
                        break;
                    default:
                        break;
                }

                SerializableStroke newStroke = new SerializableStroke { DrawingAttributes = drawingAttr };

                foreach (var stylusPoint in stroke.StylusPoints)
                {
                    newStroke.StylusPoints.Add(new SerializableStylusPoint { PressureFactor = stylusPoint.PressureFactor, X = stylusPoint.X, Y = stylusPoint.Y });
                }
                expected.Add(newStroke);
            }

            DataContractSerializer serializer = new DataContractSerializer(typeof(SerializableStrokeCollection));
            fs = new FileStream(fileName, FileMode.Create);
            serializer.WriteObject(fs, expected);
            fs.Flush();
            fs.Close();

            SerializableStrokeCollection actual;
            actual = SerializableStrokeCollection.LoadFromFile(fileName);
            Assert.AreEqual<int>(expected.Count, actual.Count);
            Assert.AreEqual<int>(expected[0].StylusPoints.Count, actual[0].StylusPoints.Count);
            Assert.AreEqual<double>(expected[0].StylusPoints[0].X, actual[0].StylusPoints[0].X);
            Assert.AreEqual<double>(expected[0].StylusPoints[0].Y, actual[0].StylusPoints[0].Y);
            Assert.AreEqual<float>(expected[0].StylusPoints[0].PressureFactor, actual[0].StylusPoints[0].PressureFactor);
            Assert.AreEqual<SerializableDrawingAttributes.StylusTips>(expected[0].DrawingAttributes.StylusTip, actual[0].DrawingAttributes.StylusTip);
            Assert.AreEqual<double>(expected[0].DrawingAttributes.Width, actual[0].DrawingAttributes.Width);

            Assert.AreEqual<int>(sc.Count, actual.Count);
            Assert.AreEqual<int>(sc[0].StylusPoints.Count, actual[0].StylusPoints.Count);
            Assert.AreEqual<double>(sc[0].StylusPoints[0].X, actual[0].StylusPoints[0].X);
            Assert.AreEqual<double>(sc[0].StylusPoints[0].Y, actual[0].StylusPoints[0].Y);
            Assert.AreEqual<float>(sc[0].StylusPoints[0].PressureFactor, actual[0].StylusPoints[0].PressureFactor);
            Assert.AreEqual<double>(sc[0].DrawingAttributes.Width, actual[0].DrawingAttributes.Width);
        }

        /// <summary>
        ///A test for SaveToFile
        ///</summary>
        [TestMethod()]
        public void SaveToFileTest()
        {
            string fileName = CommonTestConstants.StrokeCollectionDeserializationTestFileName;
            
            FileStream fs = new FileStream(Path.Combine(CommonTestConstants.RelativeTestProjectDirectory, "StrokeSerializationTest.isf"), FileMode.Open, FileAccess.Read, FileShare.Read);
            StrokeCollection expected = new StrokeCollection(fs);
            fs.Close();

            SerializableStrokeCollection target = StrokeHelper.ConvertToSerializableStrokeCollection(expected);
            target.SaveToFile(fileName);

            SerializableStrokeCollection targetFromFile = SerializableStrokeCollection.LoadFromFile(fileName);
            StrokeCollection actual = StrokeHelper.ConvertToStrokeCollection(targetFromFile);

            Assert.AreEqual<int>(expected.Count, actual.Count);
            Assert.AreEqual<int>(expected[0].StylusPoints.Count, actual[0].StylusPoints.Count);
            Assert.AreEqual<double>(expected[0].StylusPoints[0].X, actual[0].StylusPoints[0].X);
            Assert.AreEqual<double>(expected[0].StylusPoints[0].Y, actual[0].StylusPoints[0].Y);
            Assert.AreEqual<float>(expected[0].StylusPoints[0].PressureFactor, actual[0].StylusPoints[0].PressureFactor);
            Assert.AreEqual<double>(expected[0].DrawingAttributes.Width, actual[0].DrawingAttributes.Width);
        }
    }
}
